package com.bawei.module_mine.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.base.BaseApplication
import com.bawei.module_mine.R
import com.bawei.module_mine.model.CarOrderModelItem
import com.bawei.module_mine.model.InspectionOrderModelItem
import com.bawei.module_mine.model.MineServiceModel
import com.bawei.module_mine.model.NewsModelItem
import com.bawei.module_mine.model.RepairOrderModelItem
import com.bawei.module_mine.model.VisitOrderModelItem

class AuditVisitAdapter : RecyclerView.Adapter<AuditVisitAdapter.VisitViewHolder>() {
    private val visitAuditList= mutableListOf<VisitOrderModelItem>()
    fun setVisitData(data:List<VisitOrderModelItem>){
        visitAuditList.clear()
        visitAuditList.addAll(data)
        notifyDataSetChanged()
    }
    override fun getItemViewType(position: Int): Int {
        return position
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VisitViewHolder {
        return VisitViewHolder(View.inflate(parent.context, R.layout.item_car_audit, null))
    }

    override fun getItemCount(): Int {
        return visitAuditList.size
    }

    override fun onBindViewHolder(holder: VisitViewHolder, position: Int) {
        val visitOrder = visitAuditList[position]
        val colorRes = when (visitOrder.visitStatus) {
            "待审核" -> com.bawei.lib_common.R.color.color_yellow_dark
            "已通过" -> com.bawei.lib_common.R.color.color_40b223
            "已完成" -> com.bawei.lib_common.R.color.color_40b223
            "驳回" -> com.bawei.lib_common.R.color.color_examine_red
            else -> android.R.color.black // 默认颜色
        }
        when (visitOrder.visitStatus) {
            "待审核" -> holder.visitState.setBackgroundResource(R.drawable.shape_item_repair_one)
            "已通过" -> holder.visitState.setBackgroundResource(R.drawable.shape_item_repair_there)
            "已完成" -> holder.visitState.setBackgroundResource(R.drawable.shape_item_repair_there)
            "驳回" -> holder.visitState.setBackgroundResource(R.drawable.shape_item_repair_four)
            else -> android.R.color.black // 默认颜色
        }
        when(visitOrder.visitStatus){
            "已通过" -> {
                holder.visitYes.visibility = View.GONE
                holder.visitNo.visibility = View.GONE
            }
            "已完成" -> {
                holder.visitYes.visibility = View.GONE
                holder.visitNo.visibility = View.GONE
            }
            "驳回" -> {
                holder.visitYes.visibility = View.GONE
                holder.visitNo.visibility = View.GONE
            }
        }
        holder.visitState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
        holder.visitState.text = "${visitOrder.visitStatus}"
        holder.visitName.text = "访客: ${visitOrder.visitorName}"
        holder.visitTime.text = "来访时间: ${visitOrder.appointmentTime}"
        holder.visitPurpose.text = "来访事由: ${visitOrder.purpose}"
        holder.visitAudit.text =  "访客预约审核"
        holder.visitPlane.text = "张先生 -A栋1单元${visitOrder.id}"
    }
    class VisitViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val visitState: TextView = view.findViewById(R.id.item_audit_state)
        val visitAudit : TextView = view.findViewById(R.id.item_audit)
        val visitPlane: TextView = view.findViewById(R.id.item_audit_plane)
        val visitName: TextView = view.findViewById(R.id.item_audit_name)
        val visitPurpose: TextView = view.findViewById(R.id.item_audit_purpose)
        val visitTime: TextView = view.findViewById(R.id.item_audit_time)
        val visitYes: Button = view.findViewById(R.id.item_audit_yes)
        val visitNo: Button = view.findViewById(R.id.item_audit_no)
    }
    fun setupLayoutManager(recyclerView: RecyclerView) {
        val layoutManager = LinearLayoutManager(recyclerView.context)
        recyclerView.layoutManager = layoutManager
//        // 添加间距装饰（水平间距16dp，垂直间距8dp）
        val spacing = recyclerView.context.resources.getDimensionPixelSize(R.dimen.grid_spacing)
        recyclerView.addItemDecoration(GridSpaceItemDecoration(
            horizontalSpacing = spacing,  // 水平间距（左右）
            verticalSpacing = spacing / 2 // 垂直间距（上下，可根据需求调整）
        ))
        // 可选：启用预加载（提升滑动流畅性）
        recyclerView.setItemViewCacheSize(20)
    }
}